/**
 * 
 */
package com.yidin.lsc;

import java.util.List;

import com.yidin.entry.MyDynamicEntry;
import com.yidin.entry.NearbyFriendsEntry;
import com.yidin.lsc.MyDynamicAdapter.ViewHolder;
import com.yidin.util.NetImageBase;
import com.yidin.util.imageCallback;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 附近好友List适配器
 * 
 * @author cola
 * 
 */
public class NearbyFriendsAdapter extends MyBaseAdapter {
	private Context context;

	private List<NearbyFriendsEntry> userInfos;
	public NearbyFriendsAdapter(List<NearbyFriendsEntry> userInfos,Context context) {
		super(userInfos, context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.userInfos = userInfos;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.userInfos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.userInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		final int i = position;
		ViewHolder holder = null;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(
					R.layout.nearbyfriendsitem, null);

			holder = new ViewHolder();
			view.setTag(holder);
			holder.love = (Button) view.findViewById(R.id.love);
			holder.love.setVisibility(View.GONE);
			holder.tv_time = (TextView) view.findViewById(R.id.dis);
			holder.tv_nick = (TextView) view.findViewById(R.id.blindName);
			holder.tv_age = (TextView) view.findViewById(R.id.blineSexImage);
			holder.tv_content = (TextView) view.findViewById(R.id.blindContent);
			holder.icon = (ImageView) view.findViewById(R.id.itemImage);
			final ImageView iv = holder.icon;
			Bitmap bm = null;
			if (this.userInfos.get(position).PhotoUrl != null) {// 请求图标
				bm = new NetImageBase().getLogoBitmap(handler, context,
						this.userInfos.get(position).PhotoUrl,
						new imageCallback() {
							public void getImage(Bitmap bm) {
								// TODO Auto-generated method stub
								if (bm != null) {
									iv.setImageBitmap(bm);
									getIsSelected().put(i, bm);
								}
							}
						});

			}
			if (bm != null) {
				iv.setImageBitmap(bm);
				getIsSelected().put(i, bm);
			}
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// holder.tv_time.setText(userInfos.get(position).a);
		holder.tv_nick.setText(userInfos.get(position).MemberName);
		holder.tv_content.setText(userInfos.get(position).Introduction);
		holder.tv_age.setText(userInfos.get(position).Age);
		if(userInfos.get(position).Sex.equals("1"))
			holder.tv_age.setBackgroundResource(R.drawable.boy);
		
		double lat = Double.parseDouble(lscManager.getInstance().gpsManager.lat);
		double lng = Double.parseDouble(lscManager.getInstance().gpsManager.lng);
		if (lat != 0 && lng != 0) {
			String dis = lscManager.getInstance().gpsManager.GetDistance(lat,lng
					, Double.parseDouble(userInfos.get(position).WeiDu),
					Double.parseDouble(userInfos.get(position).JingDu))
					+ "";
			if(userInfos.get(position).IsOnline.equals("1"))
			holder.tv_time.setText(dis + "/km|当前在线");
			else
				holder.tv_time.setText(dis + "/km|当前不在线");
		} else {
			if (userInfos.get(position).IsOnline.equals("1"))
				holder.tv_time.setText("无位置信息|当前在线");
			else
				holder.tv_time.setText("无位置信息|当前不在线");
		}
		
		if (getIsSelected().get(position) != null)
			holder.icon.setImageBitmap(getIsSelected().get(position));
		return view;
	}

	class ViewHolder {
		TextView tv_time;
		TextView tv_nick;
		TextView tv_content;
		TextView tv_age;
		ImageView icon;
		Button love;
	}
}
